import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:note_life/color.dart';

import 'dart:async';

import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:note_life/image_info/image_check_page.dart';
import 'record_view.dart';
import 'map_view.dart';

class MainPageBody extends StatefulWidget {
  ScrollCallBack callBack;

  MainPageBody({@required this.callBack});

  @override
  State<StatefulWidget> createState() {
    return _MainPageBodyState(callBack: callBack);
  }
}

class _MainPageBodyState extends State<MainPageBody> {
  ScrollCallBack callBack;

  _MainPageBodyState({@required this.callBack});

  String oneWord = "";
  String oneWordFrom = "";

  double curNum = 0.0;
  List<String> imgList = [
    "http://e.hiphotos.baidu.com/image/h%3D300/sign=8000a165df1b0ef473e89e5eedc551a1/b151f8198618367afe76969623738bd4b21ce5fa.jpg",
    "http://a.hiphotos.baidu.com/image/h%3D300/sign=313bb7835f66d01661199828a72ad498/8601a18b87d6277f96cf067625381f30e824fc9a.jpg",
    "http://a.hiphotos.baidu.com/image/h%3D300/sign=a284ee4bc595d143c576e22343f18296/0b7b02087bf40ad182fac5ab5a2c11dfa9ecce58.jpg",
    "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2544330623,2368652634&fm=26&gp=0.jpg",
    "http://a.hiphotos.baidu.com/image/h%3D300/sign=de08a1f093510fb367197197e932c893/b999a9014c086e062550d0020f087bf40bd1cbfb.jpg",
    "http://d.hiphotos.baidu.com/image/h%3D300/sign=e6cb69522534349b6b066885f9eb1521/91ef76c6a7efce1b5ef04082a251f3deb58f659b.jpg",
    "http://h.hiphotos.baidu.com/image/h%3D300/sign=7b3e18d7c6ef7609230b9f9f1edca301/6d81800a19d8bc3ec6ff1a808f8ba61ea9d34580.jpg",
    "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1640210176,316780632&fm=27&gp=0.jpg"
  ];
  var control = ScrollController();

  @override
  void initState() {
    super.initState();
    getOneWordData();
  }

  var lastOffset = 0.0;
  var downScroll = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        controller: control
          ..addListener(() {
            if (lastOffset < control.offset && !downScroll) {
              callBack(-1);
              downScroll = !downScroll;
            } else if (lastOffset > control.offset && downScroll) {
              callBack(1);
              downScroll = !downScroll;
            }
            lastOffset = control.offset;
          }),
        child: oneWord == ""
            ? Container()
            : Column(
                children: <Widget>[
                  Container(
                    alignment: Alignment.center,
                    child: Text(
                      oneWord,
                      style: TextStyle(color: AppColor.THEME_COLOR),
                    ),
                    padding: EdgeInsets.only(left: 10.0, right: 10.0),
                  ),
                  RecordView(
                    curNum: 52.0,
                  ),
                  Container(
                    child: Row(
                      children: <Widget>[
                        Container(
                          child: Text(
                            "账单小计",
                            style: TextStyle(
                                color: AppColor.THEME_COLOR, fontSize: 16.0),
                          ),
                          padding: EdgeInsets.all(10.0),
                        ),
                      ],
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    ),
                  ),
                  ListView(
                    physics: ScrollPhysics(),
                    children: createListView(),
                    shrinkWrap: true,
                  ),
                  GestureDetector(
                    child: Container(
                      child: Text(
                        "查看全部",
                        style: TextStyle(
                            color: AppColor.THEME_COLOR, fontSize: 12.0),
                      ),
                      alignment: Alignment.center,
                      padding: EdgeInsets.all(10.0),
                    ),
                  ),
                  Container(
                    height: 150.0,
                    padding: EdgeInsets.all(10.0),
                    child: Card(
                      elevation: 4.0,
                      clipBehavior: Clip.hardEdge,
                      child: Container(
                          child: GestureDetector(
                        excludeFromSemantics: false,
                        child: IgnorePointer(
                          child: MapView(),
                        ),
                      )),
                    ),
                  ),
                  StaggeredGridView.countBuilder(
                    crossAxisCount: 4,
                    itemCount: imgList.length,
                    itemBuilder: (BuildContext context, int index) =>
                        buildPhotoView(index, context),
                    staggeredTileBuilder: (int index) =>
                        new StaggeredTile.count(
                            index == 0 ? 2 : index % 5 == 0 ? 4 : 2,
                            index.isEven ? 2 : 3),
                    mainAxisSpacing: 4.0,
                    crossAxisSpacing: 4.0,
                    shrinkWrap: true,
                    padding: EdgeInsets.all(10.0),
                    physics: ScrollPhysics(),
                  )
                ],
                crossAxisAlignment: CrossAxisAlignment.end,
              ),
      ),
    );
  }

  GestureDetector buildPhotoView(int index, BuildContext context) {
    return GestureDetector(
      child: Container(
          color: AppColor.THEME_GB_COLOR,
          child: new Card(
            child: new Column(
              children: <Widget>[
                Expanded(
                  child: ConstrainedBox(
                    child: Hero(
                      child: Container(
                        child: ClipRRect(
                          child: Image.network(
                            imgList[index],
                            fit: BoxFit.cover,
                          ),
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(5.0),
                              topRight: Radius.circular(5.0)),
                        ),
                      ),
                      tag: 'hero_img_$index',
                    ),
                    constraints: BoxConstraints.expand(),
                  ),
                  flex: 1,
                ),
                Container(
                  child: Text(
                    "2018-06-06",
                    style: TextStyle(color: AppColor.TEXT_BLACK),
                  ),
                  alignment: Alignment.centerRight,
                  padding: EdgeInsets.all(5.0),
                )
              ],
            ),
            elevation: 1.0,
          )),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => ImgCheckPage(
                  imgPath: imgList[index],
                  heroTag: 'hero_img_$index',
                )));
      },
    );
  }

  Future getOneWordData() async {
    Dio dio = new Dio();
    var stringData = await dio
        .post("http://open.iciba.com/dsapi/", data: {"date": "2013-05-03"});
    var body = stringData.data;
    print(json.decode(body));
    setState(() {
      oneWord = json.decode(body)['note'];
//      oneWordFrom = body['Body']['word_from'];
    });
  }

  List<Widget> createListView() {
    List<Widget> viewList = new List();
    for (int i = 0; i < 2; i++) {
      viewList.add(Column(
        children: <Widget>[
          i != 0
              ? Container(
                  margin: EdgeInsets.only(left: 10.0, right: 10.0),
                  child: Divider(
                    height: 2,
                  ),
                )
              : Container(),
          InkWell(
            child: Container(
              padding: EdgeInsets.all(10.0),
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.access_time,
                    color: i == 0 ? AppColor.BLUE_COLOR : AppColor.GREEN_COLOR,
                  ),
                  Expanded(
                    child: i == 0
                        ? Container(
                            margin: EdgeInsets.only(left: 5.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                  "五月份工资",
                                  style: TextStyle(
                                      color: AppColor.THEME_COLOR,
                                      fontSize: 16.0),
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 5.0),
                                  child: Text(
                                    "工资总计：7000元",
                                    style: TextStyle(
                                        color: i == 0
                                            ? AppColor.BLUE_COLOR
                                            : AppColor.GREEN_COLOR),
                                  ),
                                )
                              ],
                            ),
                          )
                        : Container(
                            margin: EdgeInsets.only(left: 5.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text(
                                  "请客吃饭消费",
                                  style: TextStyle(
                                      color: AppColor.THEME_COLOR,
                                      fontSize: 16.0),
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 5.0),
                                  child: Text(
                                    "新辣道火锅6人消费300元",
                                    style: TextStyle(
                                        color: i == 0
                                            ? AppColor.BLUE_COLOR
                                            : AppColor.GREEN_COLOR),
                                  ),
                                )
                              ],
                            ),
                          ),
                    flex: 1,
                  ),
                  Container(
                    margin: EdgeInsets.only(left: 5.0),
                    child: Text(
                      i == 0 ? "+7000" : "-300",
                      style: TextStyle(
                          color: i == 0 ? AppColor.GREEN_COLOR : Colors.red),
                    ),
                  )
                ],
                crossAxisAlignment: CrossAxisAlignment.center,
              ),
            ),
          ),
        ],
      ));
    }
    return viewList;
  }
}

typedef ScrollCallBack(int type);
